package com.miblog.domain;

import com.google.common.base.MoreObjects;
import org.springframework.data.jpa.domain.AbstractPersistable;

import javax.persistence.*;
import java.util.Set;

/**
 * 用户类
 * Created by liuzhengyang on 2015/4/4.
 */
@Entity
public class User extends AbstractPersistable<Long> {

    @Column(unique = true)
    private String username;
    private String password;

    @OneToOne(optional = true, orphanRemoval = false,cascade = CascadeType.ALL)
    @JoinColumn(name = "BLOG_ID", unique = true, nullable = true, updatable = false)
    private Blog blog;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
    private Set<Post> posts;


    @ManyToMany( fetch = FetchType.EAGER, targetEntity = User.class, cascade = CascadeType.MERGE)
    private Set<User> friends;


    public Set<User> getFriends() {
        return friends;
    }

    public void setFriends(Set<User> friends) {
        this.friends = friends;
    }

    public Set<Post> getPosts() {
        return posts;
    }

    public void setPosts(Set<Post> posts) {
        this.posts = posts;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
//                ", blog=" + blog +
//                ", posts=" + posts +// failed to lazily initialize a collection of role: com.miblog.domain.User.posts, could not initialize proxy - no Sessio
//                ", friends=" + friends +
                "} " + super.toString();
    }
}
